<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>Java reflection | 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~ | It&#39;s founded on March 9, 2019 and the open source address for the blog notes https://github.com/YUbuntu0109/YUbuntu0109.github.io</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Java">
    <meta name="description" content="Java Reflection概念Reflection(反射)是被认为动态语言的关键,反射机制允许程序在执行期间借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法.   应用动态代理 反射机制提供的功能 在运行时判断任意一个对象所属的类. 在运行时构造人任意一个类的对象. 在运行时判断任意一个类所具有的成员变量和方法. 在运行时调用任意一个对象的成员变量和">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java reflection">
<meta property="og:url" content="https://yubuntu0109.github.io/2019/04/15/Java-reflection/index.html">
<meta property="og:site_name" content="欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~">
<meta property="og:description" content="Java Reflection概念Reflection(反射)是被认为动态语言的关键,反射机制允许程序在执行期间借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法.   应用动态代理 反射机制提供的功能 在运行时判断任意一个对象所属的类. 在运行时构造人任意一个类的对象. 在运行时判断任意一个类所具有的成员变量和方法. 在运行时调用任意一个对象的成员变量和">
<meta property="og:locale" content="en">
<meta property="og:updated_time" content="2020-03-31T12:36:02.533Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java reflection">
<meta name="twitter:description" content="Java Reflection概念Reflection(反射)是被认为动态语言的关键,反射机制允许程序在执行期间借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方法.   应用动态代理 反射机制提供的功能 在运行时判断任意一个对象所属的类. 在运行时构造人任意一个类的对象. 在运行时判断任意一个类所具有的成员变量和方法. 在运行时调用任意一个对象的成员变量和">
    
        <link rel="alternate" type="application/atom+xml" title="欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/my-portrait.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">黄宇辉</h5>
          <a href="mailto:3083968068@qq.com" title="3083968068@qq.com" class="mail">3083968068@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                Homepage
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/YUbuntu0109" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://space.bilibili.com/364361791" target="_blank" >
                <i class="icon icon-lg icon-link"></i>
                Bilibili
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Java reflection</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
        <!-- add the background music on Mar 11,2019
        <div>
            <iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=280 height=52 src="//music.163.com/outchain/player?type=2&id=438801642&auto=1&height=32"></iframe>
        </div>
        -->
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Java reflection</h1>
        <h5 class="subtitle">
            
                <time datetime="2019-04-15T08:39:07.000Z" itemprop="datePublished" class="page-time">
  2019-04-15
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#Java-Reflection"><span class="post-toc-number">1.</span> <span class="post-toc-text">Java Reflection</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#概念"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">概念</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#应用"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">应用</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#反射机制提供的功能"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">反射机制提供的功能</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#反射相关的主要API"><span class="post-toc-number">1.3.1.</span> <span class="post-toc-text">反射相关的主要API</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#反射的简单应用示例"><span class="post-toc-number">1.3.2.</span> <span class="post-toc-text">反射的简单应用示例</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#详解反射的源头-Class"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">详解反射的源头 : Class</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#getClass-的使用"><span class="post-toc-number">1.4.1.</span> <span class="post-toc-text">getClass()的使用</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#获取Class类的实例的四种方式"><span class="post-toc-number">1.4.2.</span> <span class="post-toc-text">获取Class类的实例的四种方式</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#类加载器ClassLoader"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">类加载器ClassLoader</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#概念-1"><span class="post-toc-number">1.5.1.</span> <span class="post-toc-text">概念</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#类的加载过程"><span class="post-toc-number">1.5.2.</span> <span class="post-toc-text">类的加载过程</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#类加载器的应用"><span class="post-toc-number">1.5.3.</span> <span class="post-toc-text">类加载器的应用</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#newInstance-创建运行时类的对象"><span class="post-toc-number">1.6.</span> <span class="post-toc-text">newInstance()创建运行时类的对象</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#条件"><span class="post-toc-number">1.6.1.</span> <span class="post-toc-text">条件</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#示例程序"><span class="post-toc-number">1.6.2.</span> <span class="post-toc-text">示例程序</span></a></li></ol></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#通过反射调用类的完整结构"><span class="post-toc-number">1.7.</span> <span class="post-toc-text">通过反射调用类的完整结构</span></a></li><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#反射的应用之动态代理"><span class="post-toc-number">1.8.</span> <span class="post-toc-text">反射的应用之动态代理</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#动态代理介绍"><span class="post-toc-number">1.8.1.</span> <span class="post-toc-text">动态代理介绍</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#静态代理程序示例"><span class="post-toc-number">1.8.2.</span> <span class="post-toc-text">静态代理程序示例</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#动态代理程序示例"><span class="post-toc-number">1.8.3.</span> <span class="post-toc-text">动态代理程序示例</span></a></li></ol></li></ol></li></ol>
        </nav>
    </aside>


<article id="post-Java-reflection"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Java reflection</h1>
        <div class="post-meta">
            <time class="post-time" title="2019-04-15 08:39:07" datetime="2019-04-15T08:39:07.000Z"  itemprop="datePublished">2019-04-15</time>

            


            

        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <h2 id="Java-Reflection"><a href="#Java-Reflection" class="headerlink" title="Java Reflection"></a>Java Reflection</h2><h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p><em><code>Reflection(反射)</code>是被认为<code>动态语言</code>的关键,反射机制允许程序在执行期间借助于<code>Reflection API</code>取得任何类的内部信息,并能直接操作任意对象的内部属性及方法.</em>  </p>
<h3 id="应用"><a href="#应用" class="headerlink" title="应用"></a>应用</h3><p><em>动态代理</em></p>
<h3 id="反射机制提供的功能"><a href="#反射机制提供的功能" class="headerlink" title="反射机制提供的功能"></a>反射机制提供的功能</h3><ol>
<li>在运行时判断任意一个对象所属的类.</li>
<li>在运行时构造人任意一个类的对象.</li>
<li>在运行时判断任意一个类所具有的成员变量和方法.</li>
<li>在运行时调用任意一个对象的成员变量和方法.</li>
<li>生成<code>动态代理</code>(体现<code>动态语言</code>的特性).</li>
</ol>
<h4 id="反射相关的主要API"><a href="#反射相关的主要API" class="headerlink" title="反射相关的主要API"></a>反射相关的主要<code>API</code></h4><ul>
<li><em>java.lang.Class : 代表一个类.</em></li>
<li><em>java.lang.reflect.Method : 代表类的方法.</em></li>
<li><em>java.lang.reflect.Field : 代表类的成员变量.</em></li>
<li><em>java.lang.reflect.Constructor : 代表类的构造方法.</em></li>
<li><em>…</em></li>
</ul>
<h4 id="反射的简单应用示例"><a href="#反射的简单应用示例" class="headerlink" title="反射的简单应用示例"></a>反射的简单应用示例</h4><ol>
<li><p><em>首先创建一个存储用户信息的类</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.bean;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: UserInfo</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 用户信息</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 9:03:40 AM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">public</span> String name;</span><br><span class="line">	<span class="keyword">private</span> String sex;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">UserInfo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">UserInfo</span><span class="params">(String name, String sex, <span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>();</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.sex = sex;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getSex</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> sex;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setSex</span><span class="params">(String sex)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.sex = sex;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="string">"UserInfo [name="</span> + name + <span class="string">", sex="</span> + sex + <span class="string">", age="</span> + age + <span class="string">"]"</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showClassInfo</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"ClassInfo: Store the user information ."</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">introduce</span><span class="params">(String s)</span> </span>&#123;</span><br><span class="line">		System.out.println(s + <span class="string">"  my name is: "</span> + name + <span class="string">"  age: "</span> + age + <span class="string">"  sex: "</span> + sex);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><em>其次利用<code>反射</code>获取该类中的信息</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.bean.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Field;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationTargetException;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Ignore;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> pers.huangyuhui.reflection.bean.UserInfo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: UserInfoTest</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 用`反射`获取`UserInfo`类中信息</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 9:10:08 AM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestUseInfoByReflection</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 不使用反射 : 创建`UserInfo`类对象,并调用其中的属性及方法</span></span><br><span class="line">	<span class="comment">// @Test</span></span><br><span class="line">	<span class="meta">@Ignore</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">commonTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		UserInfo userInfo = <span class="keyword">new</span> UserInfo();</span><br><span class="line">		userInfo.setName(<span class="string">"Java reflection"</span>);</span><br><span class="line">		userInfo.setAge(<span class="number">1</span>);</span><br><span class="line">		userInfo.setSex(<span class="string">"Male"</span>);</span><br><span class="line">		userInfo.showClassInfo();</span><br><span class="line">		userInfo.introduce(<span class="string">"Hello !"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 使用反射: 创建`UserInfo`类对象,并调用其中的属性及方法</span></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">reflectionTest</span><span class="params">()</span> <span class="keyword">throws</span> InstantiationException, IllegalAccessException, NoSuchFieldException,</span></span><br><span class="line"><span class="function">			SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException </span>&#123;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 下述两行代码体现了: `UserInfo userInfo = new UserInfo();`的工作</span></span><br><span class="line">		Class&lt;UserInfo&gt; class1 = UserInfo<span class="class">.<span class="keyword">class</span></span>;</span><br><span class="line">		<span class="comment">// 创建运行时类`UserInfo`类的对象</span></span><br><span class="line">		UserInfo userInfo = class1.newInstance();</span><br><span class="line"></span><br><span class="line">		System.out.println(userInfo);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 通过`getField`调用别`public`修饰的`name`属性并为其赋值</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		java.lang.reflect.Field fieldName = class1.getField(<span class="string">"name"</span>);<span class="comment">// `name`被`public`修饰,否则抛出: NoSuchFieldException</span></span><br><span class="line">		fieldName.set(userInfo, <span class="string">"Java reflection"</span>);</span><br><span class="line"></span><br><span class="line">		System.out.println(userInfo);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 通过`getField`调用被`private`修饰的`age,sex`属性并为其赋值</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		Field fieldAge = class1.getDeclaredField(<span class="string">"age"</span>);</span><br><span class="line">		Field fieldSex = class1.getDeclaredField(<span class="string">"sex"</span>);</span><br><span class="line">		<span class="comment">// 将此对象的 accessible 标志设置为指示的布尔值.值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查.</span></span><br><span class="line">		<span class="comment">// 值为 false 则指示反射的对象应该实施 Java 语言访问检查.实际上setAccessible是启用和禁用访问安全检查的开关,</span></span><br><span class="line">		<span class="comment">// 并不是为true就能访问为false就不能访问 ! false时使用对象会抛出 : IllegalAccessException .</span></span><br><span class="line">		<span class="comment">// 由于JDK的安全检查耗时较多,所以通过`setAccessible(true)`的方式关闭安全检查就可以达到提升反射速度的目的 .</span></span><br><span class="line">		fieldAge.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">		fieldSex.setAccessible(<span class="keyword">true</span>);</span><br><span class="line">		fieldAge.set(userInfo, <span class="number">2</span>);</span><br><span class="line">		fieldSex.set(userInfo, <span class="string">"male"</span>);</span><br><span class="line"></span><br><span class="line">		System.out.println(userInfo);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 通过``调用对象指定的方法</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		java.lang.reflect.Method method = class1.getMethod(<span class="string">"showClassInfo"</span>);</span><br><span class="line">		<span class="comment">// return: the result of dispatching the method represented bythis object on obj</span></span><br><span class="line">		<span class="comment">// with parameters args.</span></span><br><span class="line">		method.invoke(userInfo);</span><br><span class="line">		Method method2 = class1.getMethod(<span class="string">"introduce"</span>, String<span class="class">.<span class="keyword">class</span>)</span>;</span><br><span class="line">		method2.invoke(userInfo, <span class="string">"Hello !"</span>);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><em>程序运行结果如下</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">UserInfo [name=<span class="keyword">null</span>, sex=<span class="keyword">null</span>, age=<span class="number">0</span>]</span><br><span class="line">UserInfo [name=Java reflection, sex=<span class="keyword">null</span>, age=<span class="number">0</span>]</span><br><span class="line">UserInfo [name=Java reflection, sex=male, age=<span class="number">2</span>]</span><br><span class="line">ClassInfo: Store the user information .</span><br><span class="line">Hello !  my name is: Java reflection  age: <span class="number">2</span>  sex: male</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="详解反射的源头-Class"><a href="#详解反射的源头-Class" class="headerlink" title="详解反射的源头 : Class"></a>详解反射的源头 : <code>Class</code></h3><p><em>在<code>Object</code>类中定义了以下的方法,此方法被所有子类继承 :</em><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@HotSpotIntrinsicCandidate</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">native</span> Class&lt;?&gt; getClass();</span><br></pre></td></tr></table></figure></p>
<p><em>该返回值的类型是一个<code>Class</code>类,此类是<code>Java反射</code>的源头,实际上所谓的反射从程序的运行结果来看也也很理解,既: <code>可以通过对象反射求出类的名称.</code></em></p>
<ul>
<li><em>正常方式 : 引入需要的<code>包类</code>名称 ——&gt; 通过<code>new</code>实例化 ——&gt; 取得实例化对象</em></li>
<li><em>反射方式 : 实例化对象 ——&gt; <code>getClass()</code> ——&gt; 得到完整的<code>包类</code>名称</em></li>
</ul>
<h4 id="getClass-的使用"><a href="#getClass-的使用" class="headerlink" title="getClass()的使用"></a><code>getClass()</code>的使用</h4><ol>
<li><p><em>示例程序如下</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.bean.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> pers.huangyuhui.reflection.bean.UserInfo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: TestUserInfoByReflection2</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 学习反射源头: `Class`类</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 11:58:34 AM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestUserInfoByReflection2</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getClassTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		UserInfo userInfo = <span class="keyword">new</span> UserInfo();</span><br><span class="line">		<span class="comment">// getClass() return: The Class object that represents the runtimeclass of this object.</span></span><br><span class="line">		<span class="comment">// 通过`运行时类`的对象,调用其`getClass()`返回其运行时类</span></span><br><span class="line">		Class&lt;?&gt; class1 = userInfo.getClass();</span><br><span class="line">		System.out.println(class1);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><em>程序运行结果如下</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">pers</span>.<span class="title">huangyuhui</span>.<span class="title">reflection</span>.<span class="title">bean</span>.<span class="title">UserInfo</span> //`运行时`类</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<ul>
<li><em><code>运行时类</code> : example.java ——<code>编译器(javac.exe)</code>——&gt; example.class ——<code>JVM类加载器</code>——&gt; example.class被加载到内存(此时<code>example.class</code>就是一个<code>运行时类</code>,存放在<code>缓存区</code>,且运行时类本身就是一个<code>Class</code>实例并只加载一次 !)</em></li>
</ul>
<h4 id="获取Class类的实例的四种方式"><a href="#获取Class类的实例的四种方式" class="headerlink" title="获取Class类的实例的四种方式"></a>获取<code>Class</code>类的实例的四种方式</h4><p><em>获取<code>Class</code>类的实例后我们可以进行如下的操作:</em></p>
<ol>
<li>创建对应的运行时类的对象.</li>
<li>调用对应的运行时类的完整结构( 属性,方法,构造器 ).</li>
<li>获取对应的运行时类的完整结构( 属性,方法,构造器,父类,所在的包,异常,注解… ).</li>
</ol>
<p><em>获取<code>Class</code>类实例的四种方式的示例程序如下</em><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.bean.test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> pers.huangyuhui.reflection.bean.UserInfo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: GetClassObj</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 获取`Class`类实例的四种方式</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 12:34:21 PM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">GetClassObj</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// @Test</span></span><br><span class="line">	<span class="comment">// 第一种方式: 通过调用运行时类本身的`.class`属性</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">firstWayTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		Class&lt;UserInfo&gt; class1 = UserInfo<span class="class">.<span class="keyword">class</span></span>;</span><br><span class="line">		Class&lt;String&gt; class2 = String<span class="class">.<span class="keyword">class</span></span>;</span><br><span class="line"></span><br><span class="line">		<span class="comment">// the result: class pers.huangyuhui.reflection.bean.UserInfo</span></span><br><span class="line">		System.out.println(class1);</span><br><span class="line">		<span class="comment">// the result: class java.lang.String</span></span><br><span class="line">		System.out.println(class2);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// @Test</span></span><br><span class="line">	<span class="comment">// 第二种方式: 通过`运行时类`的对象获取</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">secondWayTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		UserInfo userInfo = <span class="keyword">new</span> UserInfo();</span><br><span class="line">		Class&lt;? extends UserInfo&gt; class1 = userInfo.getClass();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// getName() return: the name of the class or interfacerepresented by this object.</span></span><br><span class="line">		<span class="comment">// the result: pers.huangyuhui.reflection.bean.UserInfo</span></span><br><span class="line">		System.out.println(class1.getName());</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">// @Test</span></span><br><span class="line">	<span class="comment">// 第三种方式: 通过`Class`的静态方法获取</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">thirdWayTest</span><span class="params">()</span></span></span><br><span class="line"><span class="function">			<span class="keyword">throws</span> ClassNotFoundException, NoSuchMethodException, SecurityException, NoSuchFieldException </span>&#123;</span><br><span class="line"></span><br><span class="line">		String className = <span class="string">"pers.huangyuhui.reflection.bean.UserInfo"</span>;</span><br><span class="line">		Class&lt;?&gt; class1 = Class.forName(className);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// the result: public java.lang.String</span></span><br><span class="line">		<span class="comment">// pers.huangyuhui.reflection.bean.UserInfo.name</span></span><br><span class="line">		System.out.println(class1.getField(<span class="string">"name"</span>));</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="comment">// 第四种方式: 通过类加载器获取</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">fourthWayTest</span><span class="params">()</span> <span class="keyword">throws</span> ClassNotFoundException, NoSuchMethodException, SecurityException </span>&#123;</span><br><span class="line"></span><br><span class="line">		String className = <span class="string">"pers.huangyuhui.reflection.bean.UserInfo"</span>;</span><br><span class="line"></span><br><span class="line">		ClassLoader classLoader = <span class="keyword">this</span>.getClass().getClassLoader();</span><br><span class="line">		<span class="comment">// Invoking this method is equivalentto invoking loadClass(name,false).</span></span><br><span class="line">		Class&lt;?&gt; class1 = classLoader.loadClass(className);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// the result: public void pers.huangyuhui.reflection.bean.UserInfo.introduce(java.lang.String)</span></span><br><span class="line">		System.out.println(class1.getMethod(<span class="string">"introduce"</span>, String<span class="class">.<span class="keyword">class</span>))</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="类加载器ClassLoader"><a href="#类加载器ClassLoader" class="headerlink" title="类加载器ClassLoader"></a>类加载器<code>ClassLoader</code></h3><h4 id="概念-1"><a href="#概念-1" class="headerlink" title="概念"></a>概念</h4><p><em>类加载器的作用:把<code>类(class)</code>装载进内存.如下示例</em><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Java源文件(*.java) ——Java编译器——&gt; 字节码文件(*<span class="class">.<span class="keyword">class</span>) ——类加载器+字节码效验器+解释器——&gt; 操作系统平台</span></span><br></pre></td></tr></table></figure></p>
<p><em><code>JVM</code>规范定义了两种类型的类加载器:<code>启动类加载器(bootstrap)</code>和<code>用户自定义加载器(user-defined class loader)</code>.JVM在运行时会产生<code>三个类加载器</code></em></p>
<ol>
<li>引导类加载器.</li>
</ol>
<ul>
<li><em><code>JVM</code>自带的类加载器,负责Java平台核心库,用来加载核心类库,该加载器无法直接获取.</em></li>
</ul>
<ol start="2">
<li>扩展类加载器.</li>
</ol>
<ul>
<li><em>负责<code>jar包</code>或将指定目录下的<code>jar包</code>装入工作库.</em></li>
</ul>
<ol start="3">
<li>系统类加载器.</li>
</ol>
<ul>
<li><em>负责<code>java-classpath</code>或<code>java.class.path</code>所指的目录下的类与<code>jar</code>包装入工作.(最常用)</em></li>
</ul>
<p><em><code>三个类加载器</code>的示例程序如下</em><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.classloader;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"><span class="keyword">import</span> pers.huangyuhui.reflection.bean.UserInfo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: ClassLoader</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 类加载器`Classloader`</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 5:37:30 PM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">TestClassLoader</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">systemClassLoaderTest</span><span class="params">()</span> <span class="keyword">throws</span> ClassNotFoundException </span>&#123;</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 获取`系统类加载器`</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		ClassLoader classLoader = ClassLoader.getSystemClassLoader();</span><br><span class="line">		<span class="comment">// getName() Returns: name of this class loader,or null if this class loader is</span></span><br><span class="line">		<span class="comment">// not named.</span></span><br><span class="line">		<span class="comment">// the result: jdk.internal.loader.ClassLoaders$AppClassLoader@28c97a5</span></span><br><span class="line">		System.out.println(classLoader);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 获取`系统类加载器`的父类加载器(`平台类加载器: getPlatformClassLoader`)</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		ClassLoader classLoader2 = classLoader.getParent();</span><br><span class="line">		<span class="comment">// the result: jdk.internal.loader.ClassLoaders$PlatformClassLoader@7bedc48a</span></span><br><span class="line">		System.out.println(classLoader2);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 查看自定义类`UserInfo`由哪个加载器加载</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		Class&lt;UserInfo&gt; class1 = UserInfo<span class="class">.<span class="keyword">class</span></span>;</span><br><span class="line">		ClassLoader classLoader3 = class1.getClassLoader();</span><br><span class="line">		<span class="comment">// the result: jdk.internal.loader.ClassLoaders$AppClassLoader@28c97a5</span></span><br><span class="line">		<span class="comment">// 由运行结果可知,该自定义类被`系统加载器`加载</span></span><br><span class="line">		System.out.println(classLoader3);</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		 * 查看`java.lang.Object`(核心类库)由哪一个加载器加载</span></span><br><span class="line"><span class="comment">		 */</span></span><br><span class="line">		Class&lt;?&gt; class2 = Class.forName(<span class="string">"java.lang.Object"</span>);</span><br><span class="line">		ClassLoader classLoader4 = class2.getClassLoader();</span><br><span class="line">		<span class="comment">// the result: null</span></span><br><span class="line">		<span class="comment">// 运行结果为`null`,说明该类被`引导类加载器`加载 !</span></span><br><span class="line">		<span class="comment">// `引导类加载器`: `JVM`自带的类加载器,负责Java平台核心库,用来加载核心类库,该加载器无法直接获取</span></span><br><span class="line">		System.out.println(classLoader4);</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="类的加载过程"><a href="#类的加载过程" class="headerlink" title="类的加载过程"></a>类的加载过程</h4><p><em>当程序主动使用某个类时,如果该类还未被<code>加载</code>到内存中,则系统会通知如下三个步骤来对该类进行初始化.</em></p>
<ol>
<li>类的加载 : 将类的<code>Class</code>文件读入到内存,并为之创建一个<code>java.lang.Class</code>对象,此过程由类加载器完成.</li>
<li>类的链接 : 将类的<code>二进制</code>数据合并到<code>JRE</code>中.</li>
<li>类的初始化 : <code>JVM</code>负责对类进行初始化.</li>
</ol>
<h4 id="类加载器的应用"><a href="#类加载器的应用" class="headerlink" title="类加载器的应用"></a>类加载器的应用</h4><ol>
<li><p><em>利用<code>ClassLoader</code>读取指定<code>包</code>下的配置文件(如果不使用类加载器配置文件只能放在<code>项目</code>目录下..).程序示例如下 :</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.classloader;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.io.InputStream;</span><br><span class="line"><span class="keyword">import</span> java.util.Properties;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: ReadConfigFileInfo</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 利用`ClassLoad`</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 15, 2019 6:29:39 PM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ReadConfigFileInfo</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">classLoaderTest</span><span class="params">()</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line"></span><br><span class="line">		Properties properties = <span class="keyword">new</span> Properties();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 如果不使用`ClassLoader`,读取当前工程下的配置文件的方式如下:</span></span><br><span class="line">		<span class="comment">// FileInputStream fileInputStream = new FileInputStream(new File("user.properties"));</span></span><br><span class="line"></span><br><span class="line">		ClassLoader classLoader = <span class="keyword">this</span>.getClass().getClassLoader();</span><br><span class="line">		<span class="comment">// return: An input stream for reading the resource.</span></span><br><span class="line">		InputStream inputStream = classLoader</span><br><span class="line">				.getResourceAsStream(<span class="string">"pers\\huangyuhui\\reflection\\classloader\\user.properties"</span>);</span><br><span class="line"></span><br><span class="line">		<span class="comment">// Reads a property list (key and element pairs) from the inputbyte stream.</span></span><br><span class="line">		properties.load(inputStream);</span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">"singer: "</span> + properties.getProperty(<span class="string">"singer"</span>) + <span class="string">"\nsong: "</span> + properties.getProperty(<span class="string">"song"</span>));</span><br><span class="line"></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><em>在<code>pers.huangyuhui.reflection.classloader</code>包下创建配置文件<code>user.properties</code></em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">singer=Justin Bieber</span><br><span class="line">song=Love yourself</span><br></pre></td></tr></table></figure>
</li>
<li><p><em>程序运行结果如下</em></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">singer: Justin Bieber</span><br><span class="line">song: Love yourself</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="newInstance-创建运行时类的对象"><a href="#newInstance-创建运行时类的对象" class="headerlink" title="newInstance()创建运行时类的对象"></a><code>newInstance()</code>创建运行时类的对象</h3><h4 id="条件"><a href="#条件" class="headerlink" title="条件"></a>条件</h4><p><em>创建类的对象: 调用<code>Class</code>对象的<code>newInstance()</code>方法,要求如下：</em></p>
<ol>
<li>类必须有一个无参数的构造器.</li>
<li>类的构造器的访问权限需要足够.</li>
</ol>
<h4 id="示例程序"><a href="#示例程序" class="headerlink" title="示例程序"></a>示例程序</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.newinstance;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.junit.Test;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> pers.huangyuhui.reflection.bean.UserInfo;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: NewInstanceTest</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 使用`newInstance()`创建运行时类的对象</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 16, 2019 10:08:37 PM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">NewInstanceTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Test</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">()</span> <span class="keyword">throws</span> InstantiationException, IllegalAccessException, ClassNotFoundException </span>&#123;</span><br><span class="line"></span><br><span class="line">		String className = <span class="string">"pers.huangyuhui.reflection.bean.UserInfo"</span>;</span><br><span class="line">		Class&lt;?&gt; class1 = Class.forName(className);</span><br><span class="line"></span><br><span class="line">		<span class="meta">@SuppressWarnings</span>(<span class="string">"deprecation"</span>)</span><br><span class="line">		<span class="comment">// 使用`newInstance()`创建对应的运行时类的对象时实际上是调用了运行时类的空参构造器.</span></span><br><span class="line">		UserInfo userInfo = (UserInfo) class1.newInstance();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// the result: UserInfo [name=null, sex=null, age=0]</span></span><br><span class="line">		System.out.println(userInfo);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="通过反射调用类的完整结构"><a href="#通过反射调用类的完整结构" class="headerlink" title="通过反射调用类的完整结构"></a>通过反射调用类的<code>完整结构</code></h3><ol>
<li>获取属性</li>
</ol>
<ul>
<li><em>略</em></li>
</ul>
<ol start="2">
<li>获取方法</li>
</ol>
<ul>
<li><em>略</em></li>
</ul>
<ol start="3">
<li>获取构造器</li>
</ol>
<ul>
<li><em>略</em></li>
</ul>
<h3 id="反射的应用之动态代理"><a href="#反射的应用之动态代理" class="headerlink" title="反射的应用之动态代理"></a>反射的应用之<code>动态代理</code></h3><h4 id="动态代理介绍"><a href="#动态代理介绍" class="headerlink" title="动态代理介绍"></a><code>动态代理</code>介绍</h4><ol>
<li>概念<br><em>动态代理是指客户通过代理类来调用其它对象的方法,并且是在程序运行时根据需要动态创建目标类的代理对象.</em></li>
<li>使用场合<br><em>调试及远程方法调用等..</em></li>
<li>设计模式的原理<br><em>使用一个代理将对象包装起来,然后使用该代理对象取代原始对象.任何对原始对象的调用都要通过代理.代理对象决定是否以及何时将方法调用转到原始对象上.</em></li>
</ol>
<h4 id="静态代理程序示例"><a href="#静态代理程序示例" class="headerlink" title="静态代理程序示例"></a><code>静态代理</code>程序示例</h4><p><em>为了与动态代理相比较,静态代理程序示例如下.不难发现: 如果添加不同接口则需要不同的代理类来完成其代理!很繁琐!!</em><br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.proxy;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">ClothFactory</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">createCloth</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//被代理类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">NikeClothFactory</span> <span class="keyword">implements</span> <span class="title">ClothFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">createCloth</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"Create the cloth .."</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//代理类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ProxyFactory</span> <span class="keyword">implements</span> <span class="title">ClothFactory</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	NikeClothFactory nikeClothFactory;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ProxyFactory</span><span class="params">(NikeClothFactory nike)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.nikeClothFactory = nike;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">createCloth</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"This is static proxy .."</span>);</span><br><span class="line">		nikeClothFactory.createCloth();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: StaticProxy</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 静态代理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 18, 2019 10:31:27 AM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StaticProxy</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		NikeClothFactory nikeClothFactory = <span class="keyword">new</span> NikeClothFactory();</span><br><span class="line">		ProxyFactory proxyFactory = <span class="keyword">new</span> ProxyFactory(nikeClothFactory);</span><br><span class="line">		proxyFactory.createCloth();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//程序运行结果如下所示 :</span></span><br><span class="line">		<span class="comment">// This is static proxy ..</span></span><br><span class="line">		<span class="comment">// Create the cloth ..</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h4 id="动态代理程序示例"><a href="#动态代理程序示例" class="headerlink" title="动态代理程序示例"></a><code>动态代理</code>程序示例</h4><ul>
<li><em><code>反射</code>是动态语言的关键,程序示例如下 :</em><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> pers.huangyuhui.reflection.proxy;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.InvocationHandler;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Method;</span><br><span class="line"><span class="keyword">import</span> java.lang.reflect.Proxy;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Subjet</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">void</span> <span class="title">action</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//被代理类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">RealSubject</span> <span class="keyword">implements</span> <span class="title">Subjet</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">action</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"被代理类执行的动作 .."</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//实现动态代理的核心类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MyInvocationHandler</span> <span class="keyword">implements</span> <span class="title">InvocationHandler</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	Object obj;<span class="comment">// 实现了接口的被代理类的对象的声明</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">// 给被代理的对象实例化</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">dynamicInit</span><span class="params">(Object object)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.obj = object;</span><br><span class="line">		<span class="comment">// `动态`地创建指定代理类的对象并返回</span></span><br><span class="line">		<span class="keyword">return</span> Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), <span class="keyword">this</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="comment">// 当通过代理类的对象发起对被重写的方法调用时,都会转换为对如下`invoke`方法的调用</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Object <span class="title">invoke</span><span class="params">(Object proxy, Method method, Object[] args)</span> <span class="keyword">throws</span> Throwable </span>&#123;</span><br><span class="line"></span><br><span class="line">		Object object = method.invoke(obj, args);</span><br><span class="line">		<span class="keyword">return</span> object;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@ClassName</span>: DynamicProxy</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Description</span>: 动态代理</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span>: HuangYuhui</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@date</span>: Apr 18, 2019 11:03:54 AM</span></span><br><span class="line"><span class="comment"> * </span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DynamicProxy</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">		MyInvocationHandler myInvocationHandler = <span class="keyword">new</span> MyInvocationHandler();</span><br><span class="line">		RealSubject realSubject = <span class="keyword">new</span> RealSubject();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// `dynamicInit`方法动态的返回一个同样实现了 `Real ...`所在类实现的接口`Subject`的代理类的对象</span></span><br><span class="line">		Object object = myInvocationHandler.dynamicInit(realSubject);</span><br><span class="line">		<span class="comment">// 此时`subject`为代理类的对象</span></span><br><span class="line">		Subjet subjet = (Subjet) object;</span><br><span class="line">		<span class="comment">// 调用`action`时会转到对`InvocationHandler`接口的实现类的`invoke`方法的调用</span></span><br><span class="line">		subjet.action();</span><br><span class="line"></span><br><span class="line">		<span class="comment">// 为证明动态代理的特性,再实现一个接口的代理类的对象</span></span><br><span class="line">		NikeClothFactory nikeClothFactory = <span class="keyword">new</span> NikeClothFactory();</span><br><span class="line">		ClothFactory clothFactory = (ClothFactory) myInvocationHandler.dynamicInit(nikeClothFactory);</span><br><span class="line">		clothFactory.createCloth();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//程序运行结果如下所示 :</span></span><br><span class="line">		<span class="comment">//被代理类执行的动作 ..</span></span><br><span class="line">		<span class="comment">//Create the cloth ..</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2020-03-31T12:36:02.533Z" itemprop="dateUpdated">2020-03-31 12:36:02</time>
</span><br>


        
    </div>
    
    <footer>
        <a href="https://yubuntu0109.github.io">
            <img src="/img/my-portrait.jpg" alt="黄宇辉">
            黄宇辉
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Java/">Java</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&title=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&pic=https://yubuntu0109.github.io/img/my-portrait.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&title=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&source=My Personal Website For Blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://yubuntu0109.github.io/2019/04/15/Java-reflection/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&via=https://yubuntu0109.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2019/04/15/Naming-Notations/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">Naming Notations</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2019/04/14/Java-annotation/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Java annotation</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        thanks ~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/Wechat_appreciates.png" alt="打赏二维码">
        </div>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        

        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>This blog is licensed under a <a rel="license" href="https://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <!-- 统计网站用户访问量. 技术支持：不蒜子(http://busuanzi.ibruce.info/) ————> Mar 13,2019 -->
        <p>
            <font style='font-size: 12px;color:springgreen'>
                    <div align="center">
                        <!-- 安装脚本 -->
                        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
                        <!-- 安装标签 -->
                        <span id="busuanzi_container_site_pv">
                            ◎用户总访问量 : <span id="busuanzi_value_site_pv"></span> 次 ~ &nbsp&nbsp
                        </span>
                        <span id="busuanzi_container_site_uv">
                            ◎总访客数(（づ￣3￣）づ╭❤～) : <span id="busuanzi_value_site_uv"></span>人 ~
                        </span>
                    </div>
                </font>
            </p>
            <!---------->
            <p>
                <font style='font-size: 10px'>
                    <span>黄宇辉 &copy; 2019 - 2020</span>
                    <span>
                        
                        Blog source <a href="https://github.com/YUbuntu0109/YUbuntu0109.github.io" target="_blank">Github</a> 
                        Power by <a href="http://hexo.io/" target="_blank">Hexo</a>
                        Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
                    </span>
                </font>
            </p>
        </div>
    </footer>
    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&title=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&pic=https://yubuntu0109.github.io/img/my-portrait.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&title=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&source=My Personal Website For Blog" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://yubuntu0109.github.io/2019/04/15/Java-reflection/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Java reflection》 — 欢迎参观小灰灰的网站哟 ヾ(◍°∇°◍)ﾉﾞ ~&url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/&via=https://yubuntu0109.github.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://yubuntu0109.github.io/2019/04/15/Java-reflection/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://yubuntu0109.github.io/2019/04/15/Java-reflection/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>








<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = 'Where are you going ?';
            clearTimeout(titleTime);
        } else {
            document.title = 'As long as you love me ~';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
